//
//  YSLTabBar.m
//  iOS-百思不得姐
//
//  Created by lanou on 16/6/29.
//  Copyright © 2016年 杨斯霖. All rights reserved.
//

#import "YSLTabBar.h"
#import "UIView+Extension.h"
@interface YSLTabBar()

//发布按钮
@property (nonatomic, strong)UIButton *publishButton;

@end

@implementation YSLTabBar

//初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        
        //设置tabBar背景图片
        [self setBackgroundImage:[UIImage imageNamed:@"whiteColor"]];
        
        //添加发布按钮
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"post_animate_add"] forState:UIControlStateNormal];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [publishButton addTarget:self action:@selector(publishAction) forControlEvents:UIControlEventTouchUpInside];
        publishButton.size = publishButton.currentBackgroundImage.size;
        [self addSubview:publishButton];
        self.publishButton = publishButton;
     
    }
    
    return self;
}


- (void)publishAction {
    SharePlatformView *pubilshView = [[SharePlatformView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) ImageArray:@[@"post_animate_album", @"post_animate_camera"] didShareButtonBlock:^(NSInteger tag) {
        
        NSLog(@"%ld", (long)tag);
    }];
    
    [pubilshView show];
    
    }

//布局子控件   通过layoutSubviews 重新布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.width;
    CGFloat height = self.height;

    
    self.publishButton.center = CGPointMake(width * 0.5, height - 40);
    
    //设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = width/5;
    CGFloat buttonH = height;
    NSInteger index = 0;
    
    for (UIView *button in self.subviews) {
        //如果不是这个UITabBarButton类型的按钮就continue 意味着只设置UITabBarButton类型的按钮
//        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")])continue;
        
    if (![button isKindOfClass:[UIControl class]] || button == self.publishButton)continue;
        
        //计算按钮的X值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        

        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        //添加索引
        index++;
        
        {
        
        }
    }
}

@end
